//
//  PublishCircleVC.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/1/31.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "PublishCircleVC.h"
#import "AppHeader.h"
#import "ImageItemView.h"
#import "XLPhotoBrowser.h"

@interface PublishCircleVC () <XLPhotoBrowserDelegate,UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet BMTextView *contentTV;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgView_height;
@property (strong, nonatomic) ImageItemView *imageView;
@property (nonatomic, strong) NSString *imageURL;
@property (nonatomic, strong) NSArray *imageArray;
@end

@implementation PublishCircleVC

-(NSArray *)imageArray
{
    if(!_imageArray)
    {
        _imageArray = [[NSArray alloc]init];
    }
    return _imageArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    ((UIScrollView *)self.view).contentSize = CGSizeMake(kWidth, kHeight);
    [self showNavigatioBarSeat:BaseBarItemDirectionRight withTitle:@"发布"];
    

    NSLog(@"%@..%@..%@", self.bgView,self.contentTV,self.view);
    
    [self.view addSubview:self.bgView];

    [self.imageView didCollectionItemBlock:^(NSInteger indexRow, BOOL isAdd) {
        if (!isAdd) {
            XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:self.selectImages currentImageIndex:indexRow];
            browser.browserStyle = XLPhotoBrowserStyleIndexLabel;
            /*
            [browser setActionSheetWithTitle:@"操作" delegate:self cancelButtonTitle:@"取消" deleteButtonTitle:nil otherButtonTitles:@"保存", nil];
             */
        }
    }];
    [self.imageView imageCountChangeBlock:^(NSMutableArray *imageArray) {
        self.selectImages = imageArray;
        [self getImageURL:self.selectImages];
    }];
    
    self.bgView_height.constant =220+KScreenWidth/4.0;
    [self.imageView viewHeightChangeBlock:^(CGFloat cellHeight) {
        self.bgView_height.constant =220+cellHeight;
    }];
}

-(void)getImageURL:(NSMutableArray *)imageArray
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i<_selectImages.count;i++) {
        UIImage *image = [[UIImage alloc]init];
        image = _selectImages[i];
        [HttpUrl upLoadImage:image hud:self.view isShow:YES WithSuccessBlock:^(id data) {
            if ([data[@"success"] integerValue] ==1) {
                if([data[@"data"] count]>0){
                    _imageURL = data[@"data"][0];
                    [array addObject:_imageURL];
                    _imageArray = [NSArray arrayWithObject:array];
                }
            }
            else
            {
                [TheGlobalMethod xianShiAlertView:data[@"msg"] controller:self];
            }
        }];
    }
   
}

-(void)barItemRightButtonClick:(UIButton *)button
{
    // 字数不能超过30
    NSLog(@"提交文字 == %@",_contentTV.text);
    NSLog(@"图片数量 == %ld",_selectImages.count);
     MKPLog(@"图片 == %@",_imageArray);
    NSMutableString * imageStr = [[NSMutableString alloc]initWithCapacity:0];
    for (int i = 0; i<[_imageArray[0] count]; i++) {
        [imageStr appendString:_imageArray[0][i]];

    if(i == [_imageArray[0] count] -1)
    {
    }else{
        [imageStr appendString:@"***"];

    }
    }
    
    //  发布
    NSMutableDictionary *dic = [[NSMutableDictionary  alloc]init];
    dic[@"content"] = _contentTV.text;
    dic[@"picUrl"] = imageStr;
    [dic setObject:@(1) forKey:@"type"];
    [HttpUrl POST:@"dynamics/addNewDynamics" dict:dic hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if ([data[@"success"] integerValue] ==1) {
            [TheGlobalMethod popAlertView:@"上传成功" controller:self];
        }
        else
        {
            [TheGlobalMethod xianShiAlertView:data[@"msg"] controller:self];
        }
    }];
    
    

}
- (ImageItemView *)imageView {
    if (!_imageView) {
        _imageView =[ImageItemView loadNib:@"ImageItemView" frame:CGRectZero];
        _imageView.itemSpace =5.0;
        _imageView.itemCount =4;
        _imageView.isDelete =YES;
        _imageView.maxCount =6;
        _imageView.sectionEdgeInsets =UIEdgeInsetsMake(0, 0, 5, 0);
        [self.bgView addSubview:_imageView];
        [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.lineView.mas_bottom).offset(3);
            make.left.mas_equalTo(self.lineView.mas_left).offset(0);
            make.right.mas_equalTo(self.lineView.mas_right).offset(0);
            make.bottom.mas_equalTo(self.bgView.mas_bottom).offset(0);
        }];
    }
    return _imageView;
}

#pragma mark - 长按下载图片
- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex {
    if (actionSheetindex ==0) {
        [browser saveCurrentShowImage];
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
